package org.dreamfly.core.service.impl;


import org.dreamfly.core.action.FileActionSupport;
import org.dreamfly.core.bean.SzRadio;
import org.dreamfly.core.dao.RadioDAO;
import org.dreamfly.core.page.PageInfo;
import org.dreamfly.core.page.PageResultSet;
import org.dreamfly.core.service.RadioService;

import java.util.List;

public class RadioServiceImpl implements RadioService {
	 private RadioDAO radioDAO;

    @Override
	    public void saveOrUpdate(SzRadio radio) {
	        radioDAO.saveOrUpdate(radio);
	    }

	    public void setRadioDAO(RadioDAO RadioDAO) {
	        this.radioDAO = RadioDAO;
	    }

		@Override
		public PageResultSet<SzRadio> normalSearch(SzRadio radio,Long pageSize, Long page, String serverName) {
			PageInfo pageInfo = null;
			List<SzRadio> list = null;
			if(serverName==null){
			pageInfo = new PageInfo(radioDAO.findCount(SzRadio.class,radio.getTitle()), pageSize, page);
			list = radioDAO.find(SzRadio.class,
													 radio.getTitle(),
													 Integer.parseInt(pageInfo.getBeginIndex().toString()), 
	    											 Integer.parseInt(pageInfo.getPageSize().toString()), null);
			}else{
				pageInfo = new PageInfo(radioDAO.findNativeCount(SzRadio.class,radio.getTitle(),serverName), pageSize, page);
				list = radioDAO.findNative(SzRadio.class,
														 radio.getTitle(),
														 serverName,
														 Integer.parseInt(pageInfo.getBeginIndex().toString()), 
		    											 Integer.parseInt(pageInfo.getPageSize().toString()));
			}
			PageResultSet<SzRadio> pageResultSet = new PageResultSet<SzRadio>();
	    	pageResultSet.setList(list);
			pageResultSet.setPageInfo(pageInfo);
			return pageResultSet;
		}
		
		@Override
		public void delete(Long id) {
			SzRadio a = radioDAO.find(SzRadio.class, id);
			FileActionSupport fas = new FileActionSupport();
			String[] filePaths = {a.getRadioUrl()};
			fas.setFilePaths(filePaths);
			try {
				fas.delete();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			radioDAO.delete(a);
		}

		@Override
		public SzRadio findDetail(Long id) {
            SzRadio radio = radioDAO.find(SzRadio.class, id);
            return radio;
		}

    @Override
    public void addRadioInfo(SzRadio radio) {
        radioDAO.saveInfo(radio);
    }

}
